package com.yeung.controller;

import com.yeung.exception.UserNotException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 这个类使用spring的beans.xml文件来创建实例并加入到ioc容器中,
 * 他需要在applicator类中添加注解  @ImportResource(locations = {"classpath:beans.xml"})  才会扫描这个beans.xml配置文件
 */
@Controller
public class HelloService {
    @Autowired
    JdbcTemplate template;

    @RequestMapping("/hello")
    public void hello(@RequestParam("user") String user) {
        if (user.equals("aaa")) throw new UserNotException();
        System.out.println("hello");
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> map() {
        String sql = "select * from department";
        List<Map<String, Object>> list = template.queryForList(sql);
        if (list.size() == 0) return null;
        return list.get(0);
    }

}
